package com.bluesmart.mia;

import java.util.Calendar;
import java.util.Date;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BabyMilkActivity extends BaseActivity implements OnClickListener{
	String mVol,mStart,mEnd;
	String mVolList[] = new String[30];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baby_milk);

        findViewById(R.id.left).setOnClickListener(this);
        findViewById(R.id.right).setOnClickListener(this);
        
        ((Button)findViewById(R.id.setting0)).setSelected(true);
        
        findViewById(R.id.setting0).setOnClickListener(this);
        findViewById(R.id.setting1).setOnClickListener(this);
        
        findViewById(R.id.layout0).setOnClickListener(this);
        findViewById(R.id.layout1).setOnClickListener(this);
        findViewById(R.id.layout2).setOnClickListener(this);
        
        for(int i = 0; i < 30; i++)
		{
        	mVolList[i] = String.valueOf(10 + i*10);
		}
        
        initDate();
	}
	
	@Override
	public void onClick(View arg0) {
		switch(arg0.getId())
		{
		case R.id.left:
			finish();
			break;
			
		case R.id.setting0:
	        ((Button)findViewById(R.id.setting0)).setSelected(true);
	        ((Button)findViewById(R.id.setting1)).setSelected(false);
	        
	        ((Button)findViewById(R.id.setting0)).setTextColor(Color.parseColor("#FFFFFF"));
	        ((Button)findViewById(R.id.setting1)).setTextColor(Color.parseColor("#0fc0db"));
	        
	        findViewById(R.id.screen).setBackgroundColor(Color.parseColor("#0D80D6"));
	        
	        ((TextView)findViewById(R.id.text0)).setText("");
	        ((TextView)findViewById(R.id.text1)).setText("");
	        ((TextView)findViewById(R.id.text2)).setText("");
			break;
			
		case R.id.setting1:
	        ((Button)findViewById(R.id.setting0)).setSelected(false);
	        ((Button)findViewById(R.id.setting1)).setSelected(true);

	        ((Button)findViewById(R.id.setting0)).setTextColor(Color.parseColor("#0fc0db"));
	        ((Button)findViewById(R.id.setting1)).setTextColor(Color.parseColor("#FFFFFF"));
	        
	        findViewById(R.id.screen).setBackgroundColor(Color.parseColor("#ade4ed"));

	        ((TextView)findViewById(R.id.text0)).setText("");
	        ((TextView)findViewById(R.id.text1)).setText("");
	        ((TextView)findViewById(R.id.text2)).setText("");
	        break;
			

		case R.id.layout0:
			showVolPicker();
			break;
			
		case R.id.layout1:
			showImagePicker(R.id.text1);
			break;
			
		case R.id.layout2:
			showImagePicker(R.id.text2);
			break;
			
		case R.id.right:
			sendChangeRequest();
			break;
		}
	}
	
	String[] yearStr;
	String[] hourStr = new String[24];
	String[] minStr = new String[60];
	void initDate()
	{
		new Thread()
		{
			public void run()
			{
				Calendar cal = Calendar.getInstance();
				cal.setTime(new Date(System.currentTimeMillis()));
				int now = cal.get(Calendar.DAY_OF_YEAR);
				
				Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
		        cal.setTime(firstDay);
		        
		        int day = now - cal.get(Calendar.DAY_OF_YEAR);
		        yearStr = new String[day + 1];
		        
				for(int i = 0; i <= day; i++)
				{
					if(i < 24) hourStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.hour);
					if(i < 60) minStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.min);

			        yearStr[i] = Utils.ConverToString(cal.getTime(), "MM-dd EE");
			        cal.add(Calendar.DAY_OF_MONTH, 1);
				}
			}
		}.start();
	}
	
	void showImagePicker(final int index)
	{
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(arg0.getId() == R.id.confirm)
				{
					WheelView year = (WheelView)mDialog.findViewById(R.id.year);
			        WheelView hour = (WheelView)mDialog.findViewById(R.id.hour);
			        WheelView min = (WheelView)mDialog.findViewById(R.id.min);
			        
			        Calendar cal = Calendar.getInstance();
					cal.setTime(new Date(System.currentTimeMillis()));
					
					Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
			        cal.setTime(firstDay);
			        cal.add(Calendar.DAY_OF_MONTH, year.getCurrentItem());
			        
			        String text = Utils.ConverToString(cal.getTime(), "yyyy-MM-dd");
			        text += " " + hourStr[hour.getCurrentItem()] + ":" + minStr[min.getCurrentItem()];
			        text = text.replace("时", "");
			        text = text.replace("分", "");
			        
					((TextView)findViewById(index)).setText(text);
					
					if(index == R.id.text1)
					{
						mStart = Utils.ConverToString(cal.getTime(), "yyyy-MM-dd") + " " + hourStr[hour.getCurrentItem()] + ":" + minStr[min.getCurrentItem()];
						mStart = mStart.replace("时", "");
						mStart = mStart.replace("分", "");
					}
					else 
					{
						mEnd = Utils.ConverToString(cal.getTime(), "yyyy-MM-dd") + " " + hourStr[hour.getCurrentItem()] + ":" + minStr[min.getCurrentItem()];
						mEnd = mEnd.replace("时", "");
						mEnd = mEnd.replace("分", "");
					}
					
					
					mDialog.dismiss();
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_date0);
		
		Date date = new Date(System.currentTimeMillis());
		
        WheelView year = (WheelView)mDialog.findViewById(R.id.year);
        year.setViewAdapter(new ArrayWheelAdapter<String>(this, yearStr,Color.parseColor("#FFFFFF")));
        year.setCurrentItem(yearStr.length - 1);
		
        WheelView hour = (WheelView)mDialog.findViewById(R.id.hour);
        hour.setViewAdapter(new ArrayWheelAdapter<String>(this, hourStr,Color.parseColor("#FFFFFF")));
        hour.setCurrentItem(date.getHours());
        
        WheelView min = (WheelView)mDialog.findViewById(R.id.min);
        min.setViewAdapter(new ArrayWheelAdapter<String>(this, minStr,Color.parseColor("#FFFFFF")));
        min.setCurrentItem(date.getMinutes());
		
		mDialog.findViewById(R.id.confirm).setOnClickListener(listen);
		mDialog.show();
	}
	
	void showVolPicker()
	{
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(arg0.getId() == R.id.confirm)
				{
					WheelView year = (WheelView)mDialog.findViewById(R.id.year);
					mVol = mVolList[year.getCurrentItem()] ;
					((TextView)findViewById(R.id.text0)).setText(mVolList[year.getCurrentItem()] + "ml");
					mDialog.dismiss();
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_vol);
		
        WheelView year = (WheelView)mDialog.findViewById(R.id.year);
        year.setViewAdapter(new ArrayWheelAdapter<String>(this, mVolList,Color.parseColor("#FFFFFF")));
        year.setCurrentItem(0);
		
		mDialog.findViewById(R.id.confirm).setOnClickListener(listen);
		mDialog.show();
	}
	
	private void sendChangeRequest()
	{
		if(mVol == null || mVol.length() == 0 || mStart == null || mStart.length() == 0 || mEnd == null || mEnd.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.feed_tip);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					ToastUtil.showToastWithAlertPic(R.string.success);
					finish();
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else
				{
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();

		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", obj.getString("babyid"));
		params.put("feedTime", mStart);
		params.put("feedEndTime", mEnd);
		params.put("feedVol", mVol);
		params.put("handType", "add");
		if(((Button)findViewById(R.id.setting0)).isSelected()) params.put("feedType", "1");
		else params.put("feedType", "2");
		
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/FeedInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
}
